package Text1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86184
 * Date: 2023-02-14
 * Time: 12:52
 */
class Animal{
    public String name;
    public int age;
    public Animal(String name,int age){
        this.name=name;
        this.age=age;
    }

    public void eat(){
        System.out.println(name+"正在吃饭");
    }
}

class Dog extends Animal{
    public Dog(String name,int age){
        super(name,age);
    }

    //重写
    @Override
    public void eat(){
        System.out.println(name+"正在吃狗粮");
    }
}

class Cat extends Animal{
    public Cat(){
        super("元宝",10);
    }

    public Cat(String name,int age){
        super(name,age);
    }

    @Override
    public void eat(){
        System.out.println(name+"正在吃猫粮");
    }
}



public class UpwardTransformation {

/*  向上转型的三种方法
    1. 直接赋值
    2. 方法传参
    3. 方法返回
*/
    public static void function1(Animal animal){
        animal.eat();
    }
    public static void main(String[] args) {
        Dog dog=new Dog("小七",10);
        Cat cat=new Cat();
        System.out.println("====第一种该方法====");
        //1.直接赋值
        Animal animal1=new Dog("铃铛",5);
        Animal animal2=new Cat("十六",5);
        animal1.eat();
        System.out.println("====================");
        animal2.eat();

        System.out.println("====第二种方法====");
        //2.方法传参
        function1(dog);
        System.out.println("=====================");
        function1(cat);

        System.out.println("=====第三种方法======");
        //3.方法返回
        function2("狗").eat();
        System.out.println("=====================");
        function2("猫").eat();
    }
    public static Animal function2(String name){
        if("狗".equals(name)){
            return new Dog("小六",11);
        }else if("猫".equals(name)){
            return new Cat("小五",11);
        }else{
            return null;
        }
    }
}
